/*    */ package com.services.bo;
/*    */ 
/*    */ import java.security.spec.KeySpec;
/*    */ import javax.crypto.Cipher;
/*    */ import javax.crypto.SecretKey;
/*    */ import javax.crypto.SecretKeyFactory;
/*    */ import javax.crypto.spec.DESedeKeySpec;
/*    */ import org.apache.commons.codec.binary.Base64;
/*    */ 
/*    */ public class DESedeEncryption
/*    */ {
/*    */   private static final String UNICODE_FORMAT = "UTF8";
/*    */   public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
/*    */   private KeySpec myKeySpec;
/*    */   private SecretKeyFactory mySecretKeyFactory;
/*    */   private Cipher cipher;
/*    */   byte[] keyAsBytes;
/*    */   private String myEncryptionKey;
/*    */   private String myEncryptionScheme;
/*    */   SecretKey key;
/*    */ 
/*    */   public DESedeEncryption(String keyId)
/*    */     throws Exception
/*    */   {
/* 25 */     this.myEncryptionKey = keyId;
/* 26 */     this.myEncryptionScheme = "DESede";
/* 27 */     this.keyAsBytes = this.myEncryptionKey.getBytes("UTF8");
/* 28 */     this.myKeySpec = new DESedeKeySpec(this.keyAsBytes);
/* 29 */     this.mySecretKeyFactory = SecretKeyFactory.getInstance(this.myEncryptionScheme);
/* 30 */     this.cipher = Cipher.getInstance(this.myEncryptionScheme);
/* 31 */     this.key = this.mySecretKeyFactory.generateSecret(this.myKeySpec);
/*    */   }
/*    */ 
/*    */   public String encrypt(String unencryptedString)
/*    */   {
/* 38 */     String encryptedString = null;
/*    */     try {
/* 40 */       this.cipher.init(1, this.key);
/* 41 */       byte[] plainText = unencryptedString.getBytes("UTF8");
/* 42 */       byte[] encryptedText = this.cipher.doFinal(plainText);
/* 43 */       return new String(Base64.encodeBase64(encryptedText), "UTF-8");
/*    */     } catch (Exception e) {
/* 45 */       e.printStackTrace();
/*    */     }
/* 47 */     return encryptedString;
/*    */   }
/*    */ 
/*    */   public String decrypt(String encryptedString)
/*    */   {
/* 53 */     String decryptedText = null;
/*    */     try {
/* 55 */       this.cipher.init(2, this.key);
/*    */ 
/* 59 */       byte[] plainText = this.cipher.doFinal(encryptedString.getBytes());
/* 60 */       decryptedText = bytes2String(plainText);
/*    */     } catch (Exception e) {
/* 62 */       e.printStackTrace();
/*    */     }
/* 64 */     return decryptedText;
/*    */   }
/*    */ 
/*    */   private static String bytes2String(byte[] bytes)
/*    */   {
/* 70 */     StringBuffer stringBuffer = new StringBuffer();
/* 71 */     for (int i = 0; i < bytes.length; i++) {
/* 72 */       stringBuffer.append((char)bytes[i]);
/*    */     }
/* 74 */     return stringBuffer.toString();
/*    */   }
/*    */ 
/*    */   public static void main(String[] args)
/*    */     throws Exception
/*    */   {
/*    */   }
/*    */ }

/* Location:           F:\BCA.jar
 * Qualified Name:     com.services.bo.DESedeEncryption
 * JD-Core Version:    0.6.0
 */